import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const express = require('express')
const employeeRouter = express.Router()
import { mysqlObj } from '../../utils/mysql.js'
import { export_state } from '../../utils/state.js'
/**
 * @api {Get} /employee 雇员首页
 * @apiGroup employee
 * @apiParam {string} [data.id=0] 雇员id
 * @apiParam {string} [data.name=""] 雇员名称
 */
employeeRouter.get('/', function (req, res) {
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" })
    res.write("<p>雇员首页</p>")
    res.end()
})

/**
 * @api {get} /employee/insert 添加雇员
 * @apiGroup employee
 * @apiParam {string} [data.id=0] 雇员id
 * @apiParam {string} [data.name=""] 雇员名称
 */
employeeRouter.get('/insert', function (req, res) {
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" })
    res.write("<p>增加雇员</p>")
    res.end()
})

/**
 * @api {get} /employee/delete 删除员工
 * @apiGroup employee
 * @apiParam {string} [data.id=0] 雇员id
 * @apiParam {string} [data.name=""] 雇员名称
 */
employeeRouter.get('/delete', function (req, res) {
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" })
    res.write("<p>删除雇员</p>")
    res.end()
})

/**
 * 根据open_id查找同家公司所有的员工
 */
employeeRouter.get('/getCompanyMember', function (req, res) {
    console.log(req.query, 'query')
    res.writeHead(200, { "content-type": "text/html;charset=utf-8" });
    // res.write("<p>得到公司的所有雇员</p>")
    let open_id = req.query.openid;
    mysqlObj.create().then((connect) => {

        let state = `select company_member from companys where company_id=(select company_id from users where open_id="${open_id}");`
        connect.query(state, function (error, results, fields) {
            if (error) throw error;
            console.log(state, 'results')

            // 获得所有该open_id的公司同事open_id
            if (JSON.stringify(results) != "[]") {

                res.write(JSON.stringify(results));
            }
            res.end();
        })
    })
})

/**
 * 根据open_id找到所在公司的群聊
 */

/**
 * @api {get} /employee/update 更新雇员
 * @apiGroup employee
 * @apiParam {string} [data.id=0] 雇员id
 * @apiParam {string} [data.name=""] 雇员名称
 */
employeeRouter.get('/update', function (req, res) {
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" })
    res.write("<p>修改雇员</p>")
    res.end()
})

/**
 * @api {get} /employee/query 查询雇员
 * @apiGroup employee
 * @apiParam {string} [data.id=0] 雇员id
 * @apiParam {string} [data.name=""] 雇员名称
 */
employeeRouter.get('/query', function (req, res) {
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" })
    res.write("<p>查询雇员</p>")
    res.end()
})
employeeRouter.get('/buildCompany', function (req, res) {


    res.writeHead(200, { "Content-type": "text/html;charset=utf8" })
    let company_name = req.query.company_name;
    // let company_avatarUrl = req.query.company_avatarUrl;
    if (company_name != "") {
        let state = `insert into companys(company_name) values('${company_name}')`;
        mysqlObj.create().then((conn) => {
            conn.query(state, function (error, results, fields) {
                console.log(results.insertId, 'sdf')
                if (JSON.stringify(results.insertId) != "") {
                    res.write(JSON.stringify(results));
                }
                res.end();
            })
        })
    }
})
/**
 * 申请加入公司
 */
employeeRouter.get('/applyIntoCompany', function (req, res) {
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" })
    res.write("<p>查询雇员</p>")
    let open_id = req.query.open_id;
    let remark = req.query.remark;
    let avatarUrl = req.query.avatarUrl;
    let company_name = req.query.companyName;
    let nickName = req.query.nickName;
    mysqlObj.create().then((conn) => {
        conn.query(`insert into applys(open_id,remark,avatarUrl,nickName, company_name) values('${open_id}','${remark}','${avatarUrl}','${nickName}', '${company_name}')`, function (error, results, field) {
            if (error) throw error;
            console.log(results, '成功');
        })
    })
    res.end()
})

/**
 * 允许加入公司
 */
employeeRouter.get('/allowInfoCompany', function (req, res) {
    res.writeHead(200, { "Content-type": "text/html;charset=utf-8" })
    res.write("<p>查询雇员</p>")
    res.end()
})
/**
 * 得到公司员工列表
 */

employeeRouter.get('/getCompanyList', function (req, res) {
    res.writeHead(200, { "content-type": "text/html;charset=utf-8" })
    mysqlObj.create().then((conn) => {
        conn.query('select * from companys', function (error, results, field) {
            if (error) throw error;
            let json = {};
            json.list = results;
            res.write(JSON.stringify(json))
            res.end();
        })
    })
})
/**
 * 得到公司申请加入公司的列表
 */
 employeeRouter.get('/getApplyList', function (req, res) {
    res.writeHead(200, { "content-type": "text/html;charset=utf-8" })
    mysqlObj.create().then((conn) => {
        conn.query('select * from applys', function (error, results, field) {
            if (error) throw error;
            let json = {};
            json.list = results;
            res.write(JSON.stringify(json))
            res.end();
        })
    })
})
/**
 * 删除单个公司列表
 */
export const employee = employeeRouter